package mapper

import (
	"gin-epg/internal/app/dao"
	"gin-epg/internal/app/entity"
)

// CreateEpgChannelAlias 创建一个新的 EpgChannelAlias
func CreateEpgChannelAlias(epgChannelAlias *entity.EpgChannelAlias) (err error) {
	if err = dao.SqlSession.Create(epgChannelAlias).Error; err != nil {
		return err
	}
	return
}

/*
* 获取 EpgChannelAlias 集合
 */
func GetAllEpgChannelAlias() (epgChannelAliasList []*entity.EpgChannelAlias, err error) {
	if err := dao.SqlSession.Find(&epgChannelAliasList).Error; err != nil {
		return nil, err
	}
	return
}

/*
* 根据 id 删除 EpgChannelAlias
 */
func DeleteEpgChannelAliasById(id string) (err error) {
	err = dao.SqlSession.Where("id=?", id).Delete(&entity.EpgChannelAlias{}).Error
	return
}

/*
*
根据name删除EpgChannelAlias
*/
func DeleteEpgChannelAliasByAlias(alias string) (err error) {
	err = dao.SqlSession.Where("alias=?", alias).Delete(&entity.EpgChannelAlias{}).Error
	return
}

/*
* 根据 id 查询 EpgChannelAlias
 */
func GetEpgChannelAliasById(id string) (epgChannelAlias *entity.EpgChannelAlias, err error) {
	// 初始化 epgChannelAlias 变量
	epgChannelAlias = new(entity.EpgChannelAlias)

	// 执行查询
	if err = dao.SqlSession.Where("id=?", id).First(epgChannelAlias).Error; err != nil {
		return nil, err
	}
	return epgChannelAlias, nil
}

/*
* 根据 name 查询 EpgChannelAlias
 */
func GetEpgChannelAliasByAlias(name string) (epgChannelAlias *entity.EpgChannelAlias, err error) {
	// 初始化 epgChannelAlias 变量
	epgChannelAlias = new(entity.EpgChannelAlias)

	// 执行查询
	if err = dao.SqlSession.Where("alias=?", name).First(epgChannelAlias).Error; err != nil {
		return nil, err
	}
	return epgChannelAlias, nil
}

/*
* 更新 EpgChannelAlias 信息
 */
func UpdateEpgChannelAlias(epgChannelAlias *entity.EpgChannelAlias) (err error) {
	err = dao.SqlSession.Save(epgChannelAlias).Error
	return
}
